package org.huawei260.TiKu;

import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.StringJoiner;
public class Hasfuman {
	// 注意类名必须为 Main, 不要有任何 package xxx 信息
//	public class Main {
		static class Node {
			Node leftChild;
			Node rightChild;
			int weightOfNode;
			int heightOfSubtree;

			public Node(Node leftChild, Node rightChild, int weightOfNode,
			            int heightOfSubtree) {
				this.leftChild = leftChild;
				this.rightChild = rightChild;
				this.weightOfNode = weightOfNode;
				this.heightOfSubtree = heightOfSubtree;
			}
		}
		public static void main(String[] args) {
			Scanner in = new Scanner(System.in);
			// 注意 hasNext 和 hasNextLine 的区别
			int n = in.nextInt();
			solution(n, in);
		}

	private static void solution(int n, Scanner in) {
		PriorityQueue<Node> priorityQueue = new PriorityQueue<>((a, b) ->
			a.weightOfNode != b.weightOfNode ?
				a.weightOfNode - b.weightOfNode : a.heightOfSubtree - b.heightOfSubtree);

		for (int k = 0; k < n; k++) {
			int j = in.nextInt();
			Node nodeCurrent = new Node(null, null, j, 0);
			priorityQueue.offer(nodeCurrent);
		}

		while (priorityQueue.size() > 1) { // 注意 while 处理多个 case
			Node leftChild = priorityQueue.poll();
			Node rightChild = priorityQueue.poll();

			int fuqinNode_wt = leftChild.weightOfNode + rightChild.weightOfNode;
			int fuqinNode_ht = rightChild.weightOfNode + 1;

			Node fuqin = new Node(leftChild, rightChild, fuqinNode_wt, fuqinNode_ht);

			priorityQueue.offer(fuqin);
		}
		Node root = priorityQueue.poll();
		StringJoiner stringJoiner = new StringJoiner(" ");

		zhongxuOrder(root, stringJoiner);

		System.out.println(stringJoiner);

	}

	static void zhongxuOrder(Node root, StringJoiner stringJoiner) {
			if (root.leftChild != null) zhongxuOrder(root.leftChild, stringJoiner);
			stringJoiner.add(root.weightOfNode + "");
			if (root.rightChild != null) zhongxuOrder(root.rightChild, stringJoiner);
		}


	}